//
//  WHMyWalletController.m
//  SquirrelCourier
//
//  Created by 邓伟浩 on 2019/1/3.
//  Copyright © 2019 邓伟浩. All rights reserved.
//

#import "WHMyWalletController.h"
#import "WHMoneyDetailController.h"
#import "WHChargeController.h"

@interface WHMyWalletController ()

@property (nonatomic, strong) UILabel *moneyLabel;

@end

@implementation WHMyWalletController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"我的钱包";
    [self setUpNavigationItem];
    [self setUpUI];
    [self selectBanlance];
    [self registerNotification];
}

- (void)registerNotification {
    [kNotificationCenter addObserver:self
                            selector:@selector(selectBanlance)
                                name:KUserDetailInfoChange
                              object:nil];
}

- (void)setUpNavigationItem {
    
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    rightButton.frame = CGRectMake(0, 0, 40, kNavBarHeight);
    NSAttributedString *attStr = [[NSAttributedString alloc] initWithString:@"明细" attributes:@{NSFontAttributeName:SYSTEMFONT(kRealwidth(16)), NSForegroundColorAttributeName:CMDarkOrangeColor}];
    [rightButton setAttributedTitle:attStr forState:UIControlStateNormal];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    [rightButton addTarget:self action:@selector(toMoneyDetail:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = rightItem;
}

- (void)setUpUI {
    UIView *topView = [[UIView alloc] init];
    [self.view addSubview:topView];
    topView.backgroundColor = KWhiteColor;
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.view);
        make.height.mas_equalTo(kRealwidth(180));
        make.top.mas_equalTo(@kTopHeight);
    }];
    
    UILabel *tipsLabel = [[UILabel alloc] init];
    tipsLabel.text = @"余额（元）";
    tipsLabel.textAlignment = NSTextAlignmentCenter;
    tipsLabel.textColor = CFontTitleBlackColor;
    tipsLabel.font = SYSTEMFONT(kRealwidth(16));
    [topView addSubview:tipsLabel];
    [tipsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(topView.mas_centerX);
        make.top.mas_equalTo(topView.mas_top).offset(kRealwidth(45));
        make.height.mas_equalTo(kRealwidth(16));
    }];
    
    UILabel *moneyLabel = [[UILabel alloc] init];
    _moneyLabel = moneyLabel;
    moneyLabel.text = @"0.00";
    moneyLabel.textAlignment = NSTextAlignmentCenter;
    moneyLabel.textColor = CFontTitleBlackColor;
    moneyLabel.font = SYSTEMFONT(kRealwidth(45));
    moneyLabel.adjustsFontSizeToFitWidth = YES;
    [topView addSubview:moneyLabel];
    [moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(topView.mas_centerX);
        make.top.mas_equalTo(tipsLabel.mas_bottom).offset(kRealwidth(25));
        make.height.mas_equalTo(kRealwidth(45));
        make.width.mas_equalTo(topView.mas_width);
    }];
    
    UIButton *chargeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    ViewRadius(chargeBtn, kRealwidth(45)/2);
    [self.view addSubview:chargeBtn];
    [chargeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(KScreenWidth-2*kRealwidth(32));
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.height.mas_equalTo(kRealwidth(45));
        make.top.mas_equalTo(topView.mas_bottom).offset(kRealwidth(20));
    }];
    [chargeBtn addTarget:self action:@selector(toCharge:) forControlEvents:UIControlEventTouchUpInside];
    NSString *chargeTitle = @"充值";
    NSAttributedString *attributeStr = [[NSAttributedString alloc] initWithString:chargeTitle attributes:@{NSFontAttributeName:SYSTEMFONT(kRealwidth(17)), NSForegroundColorAttributeName:KWhiteColor}];
    [chargeBtn setAttributedTitle:attributeStr forState:UIControlStateNormal];
    chargeBtn.backgroundColor = CMDarkOrangeColor;
}

#pragma mark - —————— 事件 ——————

- (void)toMoneyDetail:(UIButton *)button {
    WHMoneyDetailController *detail = [[WHMoneyDetailController alloc] init];
    [self.navigationController pushViewController:detail animated:YES];
}

- (void)toCharge:(UIButton *)button {
    WHChargeController *charge = [[WHChargeController alloc] init];
    [self.navigationController pushViewController:charge animated:YES];
}


#pragma mark - —————— 接口 ——————
- (void)selectBanlance {
    NSString *postURL = NSStringFormat(@"%@%@", URL_main, URL_selectBanlance);
    
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
                          @(userManager.detailUserInfo.userId), @"userId",
                          nil];
    
    [WHNetworkManager requestWithURL:postURL parameters:dict isUpData:YES Success:^(id responseObject) {
        DLog(@"%@", responseObject);
        if (ValidDict(responseObject)) {
            NSNumber *code = responseObject[@"errcode"];
            NSString *errorMsg = responseObject[@"errmsg"];
            if (code.intValue == 0) {
                NSDictionary *retDict = responseObject[@"retobj"];
                NSString *balance = retDict[@"balance"];
                _moneyLabel.text = ValidStr(balance)?balance:@"数据异常";
            } else {
                [WHView showHudMsg:errorMsg];
            }
        }
    } failure:^(NSError *error) {
        
    }];
}

@end
